package dao

import javax.inject.{Inject, Singleton}
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile
import tool.Pojo.{AdvancedFilterData, BrowseData, PageData, QueryPageData}

import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 2018/7/17
 */
@Singleton
class JunctionLocationDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def deleteAll(kind: String) = db.run(JunctionLocation.filter(_.kind === kind).delete).map(_ => ())

  def insertAll(rows: List[JunctionLocationRow]) = db.run(JunctionLocation ++= rows).map(_ => ())

  def selectAllJunctionId(data: AdvancedFilterData) = db.run(JunctionLocation.
    filter { x =>
      val chr1StartB = data.start.map { start =>
        x.chr1Pos >= start
      }.getOrElse(true.bind)
      val chr1EndB = data.end.map { end =>
        x.chr1Pos <= end
      }.getOrElse(true.bind)
      val chr2StartB = data.start.map { start =>
        x.chr2Pos >= start
      }.getOrElse(true.bind)
      val chr2EndB = data.end.map { end =>
        x.chr2Pos <= end
      }.getOrElse(true.bind)
      data.chr.map { chr =>
        (x.chr1 === chr && chr1StartB && chr1EndB) ||
          (x.chr2 === chr && chr2StartB && chr2EndB)
      }.getOrElse(LiteralColumn(true))
    }.map(_.junctionId).result)


}
